package leetcode101.ch2;

import java.util.Arrays;
import java.util.Comparator;

//LC 435
public class NonOverlappingIntev {
    public int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });

        int count =0, rightIdx = intervals[0][1];
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] < rightIdx) {
                count++;
            }else {
                rightIdx = intervals[i][1];
            }
        }
        return count;
    }
}
